package cn.com.simple.learning.rabbitmq.config;

import cn.com.simple.learning.core.constants.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jianxing.Jia
 * @date 2022/5/22
 * @description 描述信息
 */
// @Configuration
public class RabbiMQConfiguration {


    @Bean
    public Queue testQueue() {

        return new Queue(RabbitMQConstants.QUEUE, true, false, false);
    }

    @Bean
    public Exchange testExchange() {
        return new DirectExchange(RabbitMQConstants.EXCHANGE, true, false);
    }

    @Bean
    public Binding testBinding(@Qualifier(value = "testQueue") Queue testQueue,
                               @Qualifier(value = "testExchange") Exchange testExchange) {
        return BindingBuilder.bind(testQueue).to(testExchange).with(RabbitMQConstants.ROUTING_KEY).noargs();
    }

    // 延时队列
    @Bean
    public Queue delayQueue() {
        return new Queue(RabbitMQConstants.DELAY_QUEUE, true, false, false);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitMQConstants.DELAY_EXCHANGE, "x-delayed-message",true, false, args);
    }

    @Bean
    public Binding delayBinding(@Qualifier(value = "delayQueue") Queue testQueue,
                               @Qualifier(value = "delayExchange") CustomExchange delayExchange) {
        return BindingBuilder.bind(testQueue).to(delayExchange).with(RabbitMQConstants.DELAY_ROUTING_KEY).noargs();
    }

}
